<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <style>
        body {
            background-color: #b2d8f8;
        }

        .container {
            background-color: white;
            width: 350px;
            margin: 0 auto;
            padding: 15px 22px;
            border: 1px solid #ccc;
            border-radius: 15px;
            margin-top: 50vh;
            transform: translate(0, -50%);
        }

        .form-group {
            margin-bottom: 10px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
        }

        .form-group input[type="text"],
        .form-group input[type="password"] {
            width: 90%;
            padding: 15px 12px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .form-group input[type="submit"] {
            width: 100%;
            font-family: "Arial Black";
            font-size: 17px;
            font-weight: 800;
            background-color: #4490f7;
            color: white;
            border: none;
            margin-top: 15px;
            margin-bottom: 5px;
            height: 45px;
            border-radius: 9px;
            cursor: pointer;
            box-shadow: 0 6px 16px 0 rgb(78 111 242 / 30%);
        }

        .form-group input[type="submit"]:hover {
            background-color: #4662d9;
        }

        a {
            text-decoration: none;
            color: #2e58ff;
        }

        a:hover,
        a:visited,
        a:link,
        a:active {
            color: #2e58ff;
        }
    </style>
</head>

<body>
<div class="container">
    <h2>欢迎注册</h2>
    <form id="registerForm">
        <div class="form-group">
            <label>用户名：</label>
            <input type="text" name="username" id="usernameInput" placeholder="请输入用户名">
        </div>
        <div class="form-group">
            <label>密码：</label>
            <input type="password" name="password" id="passwordInput" placeholder="请输入密码">
        </div>
        <div class="form-group">
            <input id="submit" type="submit" value="注册">
        </div>
        <div style="font-size: 14px;color: #9B9B9B;text-align: right;">
            <span>已有帐号？</span>
            <a href="login.html">
                登录
            </a>
        </div>
    </form>
</div>
</body>

</html>
<script>
    document.getElementById('registerForm').addEventListener('submit', function(event) {
        event.preventDefault();

        // 获取用户名和密码输入框的值
        const username = document.getElementById('usernameInput').value;
        const password = document.getElementById('passwordInput').value;

        // 构建登录数据对象
        const registerData = {
            username: username,
            password: password
        };

        // 发起登录请求
        // fetch('http://hanfeiyu.free.idcfengye.com/user/register', {
        fetch('http://localhost/user/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(registerData)
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应不正常');
                }
                return response.json();
            })
            .then(data => {
                if (data.flag) {
                    alert('注册成功');
                    window.location.href = 'login.html';
                }else {
                    document.getElementById('usernameInput').value = "";
                    document.getElementById('passwordInput').value = "";
                    alert('注册失败，' + data.message)
                    console.error('注册失败:', data.message);
                }
            })
            .catch(error => {
                console.error('登录请求出现问题:', error);
            });
    });
</script>